package wpmp.extjs3.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wcommons.lang.JsonUtils;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 6:13:58 PM Dec 19, 2012
 */
public class TreeNode implements Serializable {

	private static final long serialVersionUID = 1L;

	private String id;

	private String text;

	private boolean leaf;

	private String icon;

	private String iconCls;

	private boolean singleClickExpand;

	private Map<String, Object> attributes = new HashMap<String, Object>();

	private List<TreeNode> children;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public boolean isLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public List<TreeNode> getChildren() {
		return children;
	}

	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getIconCls() {
		return iconCls;
	}

	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}

	public Map<String, Object> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

	public void addAttribute(String key, Object value) {
		attributes.put(key, value);
	}

	public Object getAttribute(String key) {
		return attributes.get(key);
	}

	public boolean isSingleClickExpand() {
		return singleClickExpand;
	}

	public void setSingleClickExpand(boolean singleClickExpand) {
		this.singleClickExpand = singleClickExpand;
	}

	/**
	 * 转换成extjs标准化的treeNode对象
	 * 
	 * @param treeNode
	 * @return
	 */
	public static Map<String, Object> convertToExtjsMap(TreeNode treeNode) {
		final Map<String, Object> result = new HashMap<String, Object>();
		result.put("id", treeNode.getId());
		result.put("text", treeNode.getText());
		result.put("leaf", treeNode.isLeaf());
		result.put("icon", treeNode.getIcon());
		result.put("iconCls", treeNode.getIconCls());
		result.put("singleClickExpand", treeNode.isSingleClickExpand());
		result.put("children", treeNode.getChildren());

		result.putAll(treeNode.getAttributes());

		return result;
	}

	@Override
	public String toString() {
		return JsonUtils.toJSONString(convertToExtjsMap(this));
	}

	public String toJSONString() {
		return this.toString();
	}

	public static String toJSONString(Collection<TreeNode> treeNodes) {
		if (treeNodes == null || treeNodes.isEmpty()) {
			return "[]";
		}
		final Collection<Map<String, Object>> entities = new ArrayList<Map<String, Object>>();
		for (TreeNode treeNode : treeNodes) {
			entities.add(convertToExtjsMap(treeNode));
		}

		return JsonUtils.toJSONString(entities);
	}
}
